package com.biyeshengjiuye.controller.admin;

import com.alibaba.fastjson.JSON;
import com.biyeshengjiuye.entity.Enterprise;
import com.biyeshengjiuye.service.EmploymentService;
import com.biyeshengjiuye.service.EnterpriseService;
import com.biyeshengjiuye.service.UserEnterService;
import com.biyeshengjiuye.utils.DataGridViewResult;
import com.biyeshengjiuye.utils.SystemConstant;
import com.biyeshengjiuye.vo.EnterVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/user/enterprise")
public class UserEnterController {

    @Resource
    private UserEnterService userEnterService;

    @Resource
    private EmploymentService employmentService;

    @Resource
    private EnterpriseService enterpriseService;

    /**
     * 查询企业用户列表
     *
     * @param enterVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(EnterVo enterVo, Integer check) {
        //设置分页信息
        PageHelper.startPage(enterVo.getPage(), enterVo.getLimit());
        //调用分页查询的方法
        if (check == 1){
            enterVo.setE_state("审核中");
        }
        List<Enterprise> enterLIst = userEnterService.findEnterpriseByPageById(enterVo);
        //创建分页对象
        PageInfo<Enterprise> pageInfo = new PageInfo<Enterprise>(enterLIst);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加企业用户
     *
     * @param enterprise
     * @return
     */
    @RequestMapping("/addEnterprise")
    public String addEnterprise(Enterprise enterprise) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用添加部门的方法

        if (enterpriseService.findById(enterprise.getE_id()) > 0) {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "已存在该ID企业，创建失败！");
        } else if (userEnterService.addEnterprise(enterprise) > 0) {
            map.put(SystemConstant.SUCCESS, true);//成功
            map.put(SystemConstant.MESSAGE, "添加成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

    /**
     * 修改岗位
     *
     * @param enterprise
     * @return
     */
    @RequestMapping("/updateEnterprise")
    public String updateEnterprise(Enterprise enterprise) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用修改部门的方法
        if (userEnterService.updateEnterprise(enterprise) > 0) {
            map.put(SystemConstant.SUCCESS, true);//成功
            map.put(SystemConstant.MESSAGE, "修改成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "修改失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }


    /**
     * 检查该企业下是否存在审核通过的招聘信息
     *
     * @param e_id
     * @return
     */
    @RequestMapping("/checkEnterpriseHasEmployment")
    public String checkEnterpriseHasEmployment(String e_id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用根据部门编号查询员工数量的方法
        if (employmentService.getEmploymentCountByEnterpriseId(e_id) > 0) {
            map.put(SystemConstant.EXIST, true);//存在
            map.put(SystemConstant.MESSAGE, "该企业下存在已通过审核的招聘信息，无法删除");
        } else {
            map.put(SystemConstant.EXIST, false);//不存在
        }
        return JSON.toJSONString(map);
    }


    /**
     * 删除用户
     *
     * @param e_id
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(String e_id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除部门的方法
        if (userEnterService.deleteById(e_id) > 0) {
            map.put(SystemConstant.SUCCESS, true);//成功
            map.put(SystemConstant.MESSAGE, "删除成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }


}
